/**
 * 版权所有 2009-2015 旺晓杨 peonyxy@msn.cn
 **/
package com.cn;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Collection;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import cn.ac.cintcm.sh.domain.Chapters;
import cn.ac.cintcm.sh.domain.TextArea;
import cn.ac.cintcm.sh.service.TextService;

/**
 * Servlet implementation class for Servlet: catalog
 *
 */
 public class Catalog extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
   static final long serialVersionUID = 1L;
   
 	public Catalog() {
		
		super();
	}   	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		this.doPost(request, response);
	}   
	
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setCharacterEncoding("UTF-8");
		String doClass = request.getParameter("doClass");
		if(doClass != null && doClass.equals("content"))
		{
			this.textArea(request, response);
		}
		else if(doClass != null && doClass.equals("original")){
			this.original(request, response);
		}else if(doClass != null && doClass.equals("item")){
			this.item(request, response);
		}
		else
		{
			this.chapters(request, response);
		}
		
	}   	  	  
	
	/*****
	 *  ��Ŀ
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void textArea(HttpServletRequest request, HttpServletResponse response) throws IOException{
		TextService textService = new TextService();
		String chapterId = request.getParameter("chapterId");
		if(chapterId == null || (chapterId = chapterId.trim()).equals(""))
			return;
		
		TextArea  textArea = textService.getChapter(chapterId);

		 PrintWriter out = response.getWriter();
		 
		 String str = "{hasnext:"+textArea.getHasNext()+",nextid:"+textArea.getNextId()+",hasprevious:"+textArea.getHasPrevious()+",previousid:"+textArea.getPreviousId()+",articles:"+net.sf.json.JSONArray.fromObject(textArea.getArticles()).toString()+"}";
	
		 out.print(str);
		 out.flush();
		
	}
	
	/*****
	 * �½�
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void chapters(HttpServletRequest request, HttpServletResponse response) throws IOException{
		TextService textService = new TextService();
		String searchMess = request.getParameter("searchMess");
		Collection coll = null;
		if(searchMess == null || (searchMess = searchMess.trim()).equals(""))
			coll = textService.getChaptersIndex();
		else{
			 Chapters  chapters = textService.getArticles(searchMess);
			 if(chapters != null){
				 coll = chapters.getChapters();
			 }
		}

		 PrintWriter out = response.getWriter();
		 String str = "{content:"+net.sf.json.JSONArray.fromObject(coll).toString()+"}";
	
		 out.print(str);
		 out.flush();
		
	}
	
	/**
	 * ԭ��
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void  original(HttpServletRequest request, HttpServletResponse response) throws IOException{
		TextService textService = new TextService();
		String searchMess = request.getParameter("searchMess");
		if(searchMess == null || (searchMess = searchMess.trim()).equals(""))
			return;
		 PrintWriter out = response.getWriter();
		 String str = "{articles:"+net.sf.json.JSONArray.fromObject(textService.getArticles(searchMess).getArticles()).toString()+"}";
		 out.print(str);
		 out.flush();
	 }
	
	/**
	 * ������Ų�ѯ
	 * @param request
	 * @param response
	 * @throws IOException
	 */
	public void item(HttpServletRequest request, HttpServletResponse response){
		TextService textService = new TextService();
		String itemIds = request.getParameter("itemIds");
		if(itemIds == null || (itemIds = itemIds.trim()).equals(""))
			return;
		 PrintWriter out = null;
		 String str = "";
			
			try {
				 out = response.getWriter();
				  str = "{articles:"+net.sf.json.JSONArray.fromObject(textService.getChapters(itemIds.split(","))).toString()+"}";
			
			} catch (Exception e) {
				str = "{articles:[{'id':'','text':'<font color=red>�������Ŵ���!</font>'}]}";
			}
		 out.print(str);
		 out.flush();
	}
}